package com.lovo.service.impl;

import com.lovo.dao.ClassDAO;
import com.lovo.dto.ClassDTO;
import com.lovo.enums.ClassCodeEnum;
import com.lovo.exception.ClassException;
import com.lovo.model.ClassDO;
import com.lovo.model.StudentDO;
import com.lovo.service.ClassService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @title
 * @author: luo
 * @date: 2020/11/22 13:38
 */
@Service
public class ClassServiceImpl implements ClassService {

    @Resource
    private ClassDAO classDAO;

    @Override
    public List<ClassDTO> findAllClass() {
        List<ClassDTO> list = classDAO.findAllStudentAndClass();
        if (list == null || list.size() <=0) {
            throw new ClassException(ClassCodeEnum.NO_DATA);
        }
        return list;
    }

    @Override
    public void deleteClass(Integer classId) {
        List<StudentDO> list = classDAO.findStudentByClassId(classId);
        if (list != null && list.size()>0) {
            classDAO.deleteClass(classId);
        }else {
            throw new ClassException(ClassCodeEnum.FAIL_DELETE_CLASS);
        }

    }

    @Override
    public void addClass(ClassDO classDO) {
        int i = classDAO.addClass(classDO);
        if (i<=0) {
            throw new ClassException(ClassCodeEnum.ADD_CLASS_FAIL);
        }
    }

    @Override
    public ClassDTO findClassById(Integer classId) {
        ClassDTO classDTO = classDAO.findClassById(classId);
        if (classDTO == null) {
            throw new ClassException(ClassCodeEnum.NO_DATA);
        }
        return classDTO;
    }

    @Override
    public void updateClass(ClassDTO classDTO) {
        int i = classDAO.updateClass(classDTO);
        if (i<=0) {
            throw new ClassException(ClassCodeEnum.FAIL_UPDATE_CLASS);
        }
    }
}
